package com.hansming.bss.modules.sfc.wxserve;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONArray;
import org.activiti.engine.impl.util.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.JsonObject;
import com.hansming.bss.common.web.BaseController;

@Controller
@RequestMapping(value = "${frontPath}/serve")
public class WxserveController extends BaseController{
		
	
		@RequestMapping(value="/reponse")
		public String  wxserveauth(HttpServletRequest request, HttpServletResponse response,String signature,String timestamp,String nonce,String echostr){
//			 String signature1 = request.getParameter("signature");
//		        // 随机字符串
//		        String echostr1 = request.getParameter("echostr");
//		        // 时间戳
//		        String timestamp1= request.getParameter("timestamp");
//		        // 随机数
//		        String nonce1 = request.getParameter("nonce");
//
//		        String[] str = { "sdajfhakhdask", timestamp1, nonce1 };
//		        Arrays.sort(str); // 字典序排序
//		        String bigStr = str[0] + str[1] + str[2];
//		        if(bigStr.equals(echostr1)){
//		        	System.out.println("相同");
//		        }else{
//		        	System.out.println("不相同");
//		        }
			
			return renderString(response, echostr, "text/html");
		}
	
	
		@RequestMapping(value="/auth")
		public void wxserveresponse(HttpServletRequest request, HttpServletResponse response,String ToUserName,String FromUserName,Integer CreateTime,String MsgType,String Content,String MsgId) throws IOException{
			response.setCharacterEncoding("UTF-8");
	        response.setHeader("Cache-Control", "no-cache");
	        PrintWriter out = response.getWriter();
	        
	        WxserveUtil.sendServe(ToUserName,"");
	        String requestStr = WxserveUtil.getStringInputStream(request);		
			System.out.println("接受的信息"+requestStr);
			Object obj = JSONObject.stringToValue(requestStr);
			
	        //返回给服务器固定信息，让其把信息返回给客服后台
	        JSONObject jsonObject = new JSONObject(); 
	        System.out.println(ToUserName+"======"+FromUserName+"======"+CreateTime+"======"+MsgType+"======"+Content);
	        System.out.println("==============进如微信客服服务回调=========");
	        jsonObject.put("ToUserName",FromUserName );
	        jsonObject.put("FromUserName", ToUserName);
	        Date now = new Date();	        
	        jsonObject.put("CreateTime",now.getTime() );
	        jsonObject.put("MsgType","transfer_customer_service" );
			out.print(jsonObject.toString());
			out.flush();
			out.close();
		}
	
	
}
